package com.zk.lib_service.service

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.content.getSystemService
import com.gc.logger.LogUtils
import com.zk.lib_service.R
import com.zk.lib_service.ServiceManager
import com.zk.lib_service.activity.ServiceStudyActivity

/**
 * 每隔3秒打印一条日志
 */
class ForegroundBinderService : Service() {
    private val binder = MusicServiceBinder(this)
    private val mHandler: Handler = Handler(Looper.getMainLooper())
    override fun onCreate() {
        super.onCreate()
        LogUtils.e("aaaaaaaaa", "ForegroundBinderService  onCreate ")
        mHandler.post(backgroundRunnable)
        startForeground()
    }

    private var backgroundRunnable: Runnable = Runnable {
        LogUtils.e("aaaaaaaaaaa", "ForegroundBinderService backgourndRunnalbe is Alive poccess id = ${android.os.Process.myPid()}")
        ServiceManager.getRunServiceInfos(this)
        reExecte()
    }
    private  val CHANNEL_FOREGROUND = "foreground-notification"
    private fun createChannelIfNeeded(context: Context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return

        val foregroundChannel =
            NotificationChannel(CHANNEL_FOREGROUND, "前台服务", NotificationManager.IMPORTANCE_LOW)
                .apply {
                    setShowBadge(false)
                    enableLights(false)
                    enableVibration(false)
                    lockscreenVisibility = Notification.VISIBILITY_SECRET
                }
        context.getSystemService<NotificationManager>()
            ?.createNotificationChannel(foregroundChannel)
    }
    fun startForeground() {

        createChannelIfNeeded(this)
        val pendingIntent = PendingIntent.getActivity(
            this,
            0,
            Intent(this, ServiceStudyActivity::class.java),
            PendingIntent.FLAG_IMMUTABLE
        )
        val notification = NotificationCompat.Builder(this, CHANNEL_FOREGROUND)
            .setSmallIcon(R.drawable.car)
            .setContentText("提示内容")
            .setContentIntent(pendingIntent)
            .setLocalOnly(true)
            .setPriority(NotificationCompat.PRIORITY_MIN)
            .setCategory(NotificationCompat.CATEGORY_SERVICE)
            .setVisibility(NotificationCompat.VISIBILITY_SECRET)
            .setOngoing(true)
            .setShowWhen(false)
            .build()
        startForeground(233, notification)
    }


    private fun reExecte() {
        mHandler.postDelayed(backgroundRunnable,3000)
    }

    override fun onDestroy() {
        LogUtils.e("aaaaaaaaa", "ForegroundBinderService  onDestroy ")
//        ServiceManager.startService(this)
        super.onDestroy()
        val manager =
            getSystemService(Service.NOTIFICATION_SERVICE) as? NotificationManager
        manager?.cancel(233)
        stopForeground(true)
        mHandler.removeCallbacks(backgroundRunnable)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        LogUtils.e("aaaaaaaaa", "ForegroundBinderService  onStartCommand ")
//        return super.onStartCommand(intent, flags, startId)
        if(intent==null){
            mHandler.removeCallbacks(backgroundRunnable)
            mHandler.post(backgroundRunnable)
            LogUtils.e("aaaaaaaaa", "ForegroundBinderService  onStartCommand 自己启动了")
        }
        return START_STICKY
    }

    override fun onBind(intent: Intent?): IBinder? {
        LogUtils.e("aaaaaaaaa", "ForegroundBinderService  onBind ")
        return binder
    }

    override fun onUnbind(intent: Intent?): Boolean {
        LogUtils.e("aaaaaaaaa", "ForegroundBinderService  onUnbind ")
        return super.onUnbind(intent)
    }
}